<?php

namespace App\Jobs\Stats\Retention\Day\Elasticsearch;

use App\Jobs\Stats\Retention\Day\Sign as JobStats;
use App\Jobs\Stats\Traits\DatabaseElasticsearch;
use App\Models\Elasticsearch\Log\Login;

class Sign extends JobStats
{
    use DatabaseElasticsearch;

    /**
     * 获取玩家人数
     *
     * @param int $timed
     * @param string $platCode
     * @param array $ids
     * @return int
     */
    protected function getUserCount(int $timed, string $platCode, array $ids): int
    {
        $params = ['body' => ['size' => 0, 'track_total_hits' => false]];
        // 筛选【UID】
        $params['body']['query']['bool']['must'][] = [
            'range' => [
                'uid' => ['gte' => $ids['id_min'], 'lte' => $ids['id_max']]
            ]
        ];
        // 筛选【时间】
        $this->makeWhereDatetime($params, 'login_time', [$timed, $timed + 86400], Login::INDEX_NAME);
        // 筛选【平台】
        $this->makeWhere($params, 'plat_code', $platCode);

        // 统计参数
        $params['body']['aggs'] = $this->makeDistinct('uid');

        // 查询数据
        return Login::search($params)['aggregations']['distinct_uid']['value'];
    }
}
